/* $Id: IconifiedTextListAdapter.java,v 1.3 2010/03/23 07:19:53 wuqf Exp $
 * 
 * Copyright 2007 Steven Osborn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lenovo.leos.filebrowser.ui;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.lenovo.leos.filebrowser.R;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

/** @author Steven Osborn - http://steven.bitsetters.com */
public class IconifiedTextListAdapter extends BaseAdapter implements OnCheckedChangeListener{

	/** Remember our context so we can use it when constructing views. */
	private Context mContext;
	private LayoutInflater mInflater;

	private List<IconifiedText> mItems = null;
	private static final String TAG = "IconifiedTextListAdapter";
	boolean flag = false;
	int checkedPos;
	boolean mChecked = false;
	
	public IconifiedTextListAdapter(Context context, List<IconifiedText> mItems) {
		mContext = context;
		mInflater = LayoutInflater.from(context);
		this.mItems = new ArrayList<IconifiedText>();
		
		if(mItems != null)
		    this.mItems.addAll(mItems);
	}

	public void setListItems(List<IconifiedText> lit) { 
		mItems.clear();
		
		if(lit != null)
		    mItems.addAll(lit); 		
	}

	
	/** @return The number of items in the */
	public int getCount() { return mItems.size(); }

	public Object getItem(int position) { return mItems.get(position); }

	public boolean areAllItemsSelectable() { return false; }


	/** Use the array index as a unique id. */
	public long getItemId(int position) {
		return position;
	}

/*	private class OnItemClickListener implements OnClickListener{           
      private int mPosition;
      public OnItemClickListener(int position){
           mPosition = position;
        }
      @Override
      public void onClick(View arg0) {
           Log.v(TAG, "********************* onItemClick at position" + mPosition);                      
        }               
    }*/
		
//		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
		//	mChecked = isChecked;
			ViewHolder holder = (ViewHolder) buttonView.getTag();
			if(isChecked && !FileBrowser.MarkedFile.contains(mItems.get(holder.position).getFile())){
				FileBrowser.MarkedFile.add(mItems.get(holder.position).getFile());
				FileBrowser.isSelected = true;
			}else if(!isChecked && FileBrowser.MarkedFile.contains(mItems.get(holder.position).getFile())){
				FileBrowser.MarkedFile.remove(mItems.get(holder.position).getFile());
				if(FileBrowser.MarkedFile.isEmpty())
					FileBrowser.isSelected = false;
			}		
			   if(FileBrowser.MarkedFile.size() > 0 && FileBrowser.MarkedFile.size() < IconifiedTextListAdapter.this.getCount()){
					FileBrowser.btnCancleAll.setEnabled(true);
					FileBrowser.btnSelectAll.setEnabled(true);
				}else if(FileBrowser.MarkedFile.size() < 0){
					FileBrowser.btnCancleAll.setEnabled(false);
					FileBrowser.btnSelectAll.setEnabled(false);
				}else if(FileBrowser.MarkedFile.size() == 0){
					FileBrowser.btnCancleAll.setEnabled(false);
					FileBrowser.btnSelectAll.setEnabled(true);
				}else if(FileBrowser.MarkedFile.size() == IconifiedTextListAdapter.this.getCount()){
					FileBrowser.btnCancleAll.setEnabled(true);
					FileBrowser.btnSelectAll.setEnabled(false);
				}

		}
	//}
							
	/** @param convertView The old view to overwrite, if one is passed
	 * @returns a IconifiedTextView that holds wraps around an IconifiedText */
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		final int pos = position;
		if (convertView == null) {					
			convertView = mInflater.inflate(R.layout.listmainitem, null);
			holder = new ViewHolder();						
			holder.checkbox = (CheckBox) convertView.findViewById(R.id.checkboxLayout);
			holder.checkbox.setFocusable(false);
			holder.checkbox.setOnCheckedChangeListener(this);//new checkedChanged(position));
			holder.checkbox.setTag(holder);
			holder.icon = (ImageView) convertView.findViewById(R.id.icon);
			holder.text = (TextView) convertView.findViewById(R.id.text);
			holder.position = pos;
			if (FileBrowser.State == FileBrowser._SELECT){
			//	Log.i(TAG, "&&&&&&&&&&&&&&&&&&&&&&&&& MarkedFile");
				if (FileBrowser.MarkedFile.contains(mItems.get(pos).getFile())){
					holder.checkbox.setChecked(true);
				}else{
					holder.checkbox.setChecked(false);
				}
			}
			convertView.setTag(holder);
		} else {
		//	Log.i(TAG, "&&&&&&&&&&&&&&&&&&&& position = "+position);
			holder = (ViewHolder) convertView.getTag();
			holder.position = pos;
			holder.checkbox.setTag(holder);
			if (FileBrowser.State == FileBrowser._SELECT){
			//	Log.i(TAG, "&&&&&&&&&&&&&&&&&&&&&&&&& MarkedFile");
				if (FileBrowser.MarkedFile.contains(mItems.get(pos).getFile())){
					holder.checkbox.setChecked(true);
				}else{
					holder.checkbox.setChecked(false);
				}
			}
		}				
	
		if (FileBrowser.State == FileBrowser._SELECT) {
			holder.checkbox.setVisibility(View.VISIBLE);			
		}else{
			holder.checkbox.setVisibility(View.GONE);
		}
			
		if(FileBrowser.cut_copy && FileBrowser.opType == FileBrowser.OPTYPE.CUT && mItems.get(pos).getIsCut()){
			holder.text.setTextColor(Color.RED);//Color.DKGRAY
		}else{
			holder.text.setTextColor(Color.WHITE);
		}
		
		String s = mItems.get(pos).getText();
		if (s.startsWith("/")){
			s = s.replaceFirst("/", "");
		}
			
		holder.text.setText(s);
		holder.icon.setAdjustViewBounds(true);
		holder.icon.setImageDrawable(mItems.get(pos).getIcon());
		
		return convertView;
	}
	

	public class ViewHolder {
		//MyCheckBox checkbox;
		CheckBox checkbox;
		ImageView icon;
		TextView  text;
		int       position;
	//	View      divider;
	}
}
